package com.ghost;

import java.io.ByteArrayOutputStream;
import java.net.ContentHandler;

import org.loon.framework.android.game.core.graphics.LImage;
import org.loon.framework.android.game.core.resource.LPKResource;

import org.loon.framework.android.game.utils.GraphicsUtils;
 
 
 
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
 


 
 
 


public class Images {

	private static Images imagefactory;

	private static LImage images[];

	private Images() {

		images = new LImage[17];
		images[0] = GraphicsUtils.loadImage("res/"   + "blank.png",false);
		images[1] = GraphicsUtils.loadImage("res/"   + "mirror_left.png",false);
		images[2] = GraphicsUtils.loadImage("res/"   + "mirror_right.png",false);
		images[3] = GraphicsUtils.loadImage("res/"   + "vampire.png",false);
		images[4] = GraphicsUtils.loadImage("res/"   + "ghost.png",false);
		images[5] = GraphicsUtils.loadImage("res/"   + "zombie.png",false);
		
		 
		for (int i = 6; i < 8; i++) {
			images[i] = GraphicsUtils.loadImage("res/" + i + ".jpg",false);
		}

		final String res="res/res.lpk";
		 
		images[8] = LPKResource.openImage(res, "a0.png");
		images[9] = LPKResource.openImage(res, "dot.png");
//		images[10] = LPKResource.openImage(res, "background.jpg");
		images[10] = GraphicsUtils.loadImage("res/"   + "background.png",false);
		images[11] = LPKResource.openImage(res, "role0.png");
		images[12] = LPKResource.openImage(res, "role1.png");
		images[13] = LPKResource.openImage(res, "role2.png");
		images[14] = LPKResource.openImage(res, "win.png");
		images[15] = LPKResource.openImage(res, "start.png");
		images[16] = LPKResource.openImage(res, "gameover.png");
	
	}

	public LImage getImage(int i) {
		return images[i];
	}
	public LImage getImage(int Rname,boolean isdrawable) {
	    LImage tempimage;
	    if (isdrawable) {
		Resources res =new Resources(null,null,null) ;
//	    Resources res= new Resources().getNotCacheResourceAsStream(R.drawable.background);
	    
//	    res=  getResources();
	    Bitmap bmp=BitmapFactory.decodeResource(res, Rname);  
	     tempimage=GraphicsUtils.loadImage(Bitmap2Bytes(bmp), false);
	    }
	    else {
		 return null;
	    }
		return tempimage;
	}
	
	public LImage getImage(String filename) {
	    LImage tempimage=GraphicsUtils.loadImage(filename,false);  
		return tempimage;
	}

	public static synchronized Images getInstance() {
		if (imagefactory != null) {
			return imagefactory;
		} else {
			imagefactory = new Images();
			return imagefactory;
		}
	}
	private byte[] Bitmap2Bytes(Bitmap bm){
		ByteArrayOutputStream baos = new ByteArrayOutputStream();  
		  bm.compress(Bitmap.CompressFormat.PNG, 100, baos); 
		 return baos.toByteArray();  
		  }  

	    

}
